/**
 *  Copyright (c) 2011, IPC Studio, USTC, All rights reserved
 *  FileName: TestApp.cpp
 *  Author: chengximing1989@gmail.com
 *  DateCreated: 2011-8-8
 */

#include "../include/TestApp.h"
#include "../include/Log.h"

TestApp::TestApp(void)
{
	m_pcmdparam = NULL;
	m_plyrics = NULL;
}

TestApp::~TestApp(void)
{
	if (m_pcmdparam)
		delete m_pcmdparam;
	if (m_plyrics)
		delete m_plyrics;
}

bool TestApp::AppInit(int argc, char** argv)
{
	m_pcmdparam = new CmdParam();
	if(!m_pcmdparam->InitParam(argc, argv))
		return false;
	//Add judgment to test the file extension (in this version, we only support *.lrc)
	string sifilename = m_pcmdparam->m_sifilename;
	//Fixed the bug which -h make the application crash
	if(sifilename.empty())
		return true;
	if(sifilename.length() < 4)
	{
		liblyrics::CLog::LogDefault(LOG_ERROR, "Input file name string length less than 4, app exit!\n");
		return false;
	}
	transform(sifilename.begin(), sifilename.end(), sifilename.begin(), (int(*)(int))tolower);
	if(sifilename.substr(sifilename.length() - 3, 3) == "lrc")
		m_plyrics = new CLrc();
	else
	{
		//cout<<"Error : Not supported file extension"<<endl;
		liblyrics::CLog::LogDefault(LOG_ERROR, "Not supported file extension\n");
		return false;
	}
	return true;
}

void TestApp::Process()
{
	if(!m_pcmdparam->m_sifilename.empty())
	{
		if(!m_plyrics->OpenLyrics(m_pcmdparam->m_sifilename))
			return;
		if(m_pcmdparam->m_itime != -1)
		{
			string out = m_plyrics->GetLrctxt(m_pcmdparam->m_itime);
			cout<<m_pcmdparam->m_itime<<" ms : "<<out<<endl;
		}
		if(m_pcmdparam->m_bprintheadinfo)
			PrintHeadInfo();
		if(!m_pcmdparam->m_sofilename.empty())
			SaveOutputFile();
		if(m_pcmdparam->m_bprintall)
			PrintAll();
	}
	else if((m_pcmdparam->m_bprintall)||(m_pcmdparam->m_bprintheadinfo))
		//cout<<"Warning : no input file, no output to print!"<<endl;
		liblyrics::CLog::LogDefault(LOG_WARNING, "no input file, no output to print!\n");

	if(m_pcmdparam->m_bprinthelp)
		PrintHelp();
	if(m_pcmdparam->m_bprintversion)
	{
		string out = m_plyrics->GetLibVersion();
		cout<<out<<endl;
	}
}

void TestApp::SaveOutputFile()
{
	FILE* fout = fopen(m_pcmdparam->m_sofilename.c_str(), "w");
	if(!fout)
	{
		//cout<<"Can not open file "<<m_pcmdparam->m_sofilename.c_str()<<" to write"<<endl;
		string logmsg = "Can not open file ";
		logmsg += m_pcmdparam->m_sofilename.c_str();
		logmsg += " to write\n";
		liblyrics::CLog::LogDefault(LOG_ERROR, logmsg.c_str());
		return;
	}
	vector<LrcRecord> lrcrec = m_plyrics->GetLrcRecord();
	vector<LrcRecord>::iterator itVector;
	for(itVector = lrcrec.begin(); itVector != lrcrec.end(); itVector++)
	{
		string txt = itVector->lrctxt;
		txt += "\n";
		fprintf(fout, txt.c_str());
	}
	fclose(fout);
}

void TestApp::PrintAll()
{
	vector<LrcRecord> lrcrec = m_plyrics->GetLrcRecord();
	vector<LrcRecord>::iterator itVector;
	for(itVector = lrcrec.begin(); itVector != lrcrec.end(); itVector++)
		cout<<itVector->lrctxt<<endl;
}

void TestApp::PrintHeadInfo()
{
	vector<string> headinfo = m_plyrics->GetLrcHeadInfo();
	vector<string>::iterator itVector;
	for(itVector = headinfo.begin(); itVector != headinfo.end(); itVector++)
		cout<<itVector->c_str()<<endl;
}

void TestApp::PrintHelp()
{
	string out = "-i filename: input *.lrc lyrics file name\n";
	out       += "-o filename: output normal text lyrics file\n";
	out       += "-t   mstime: input the time in millisecond, print the lyrics at that time\n";
	out       += "-H         : print the head information of the input lyrics\n";
	out       += "-a         : print all lyrics in time sequence\n";
	out       += "-v         : print the version of this application\n";
	out       += "-h         : print help text";
	cout<<out<<endl;
}
